﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Skylabs.ACL
{
    public class ICommandParse
    {
        public class List : Arrays.BaseList
        {
            public string name;
            public struct SUBSETS
            {
                public string name;
                public string eq;
            }
            public SUBSETS[] subsets = new SUBSETS[] { };
        }
        public class COMMANDARRAY : Arrays.Astringindex<List>
        {
            private string _name;
            private string _raw_command;
            private bool _success;
            public string name
            {
                get
                {
                    return _name;
                }
            }
            public string raw_command
            {
                get
                {
                    return _raw_command;
                }
            }
            public bool success
            {
                get
                {
                    return this._success;
                }
            }
            public COMMANDARRAY()
            {

            }
            public COMMANDARRAY(string strCommand)
            {
                this.ParseCommands(strCommand);
            }
            public void ParseCommands(string strCom)
            {
                Regex rex;
                string strTemp = "";
                this._raw_command = strCom;
                this._success = false;
                if (strCom.Length > 0)
                {
                    if (strCom[0] == '/'){
                        //"/commandname -subcommand subset=subeq"
                        //remove /
                        this.list = new List[] { };
                        strCom = strCom.TrimStart(new char[1] { '/' });
                        //get command name 
                        rex = new Regex("^([?a-zA-Z_]+[0-9]*)+");
                        if (rex.IsMatch(strCom))
                        {
                            this._name = rex.Match(strCom).Value;
                            this._success = true;
                            strCom = rex.Replace(strCom, "", 1);

                        }
                        //get subcommand,subset,subeq
                        rex = new Regex(" *[-]{1}(?<a>[^-]+)*", RegexOptions.ExplicitCapture);
                        while (rex.IsMatch(strCom))
                        {
                            strTemp = Regex.Match(rex.Match(strCom).Value, "-(?<a>[a-zA-Z_]+[0-9]*)+", RegexOptions.ExplicitCapture).Groups[0].Value;
                            this[strTemp.TrimStart(new char[1] { '-' })].name = strTemp.TrimStart(new char[1] { '-' });
                            parse_subcommand(rex.Match(strCom).Groups["a"].Value, strTemp.TrimStart(new char[1] { '-' }));
                            string str = rex.Match(strCom).Value;
                            strCom = strCom.Replace(rex.Match(strCom).Value, "");
                        }
                    }
                }
            }
            private void parse_subcommand(string subcom_raw, string strSubCom)
            {
                Regex rex = new Regex("((?<subset>[^ \'=\"]+)|(?<subset>[\'\"]{1}[^\'\"]*[\'\"]{1})){1}( *= *((?<subeq>[^ \'=\"]+)|(?<subeq>[\'\"]{1}[^\'\"]*[\'\"]{1})){1}){0,1} *", RegexOptions.ExplicitCapture);
                subcom_raw = subcom_raw.Substring(strSubCom.Length);
                MatchCollection matches = rex.Matches(subcom_raw);
                int i = this[strSubCom].subsets.Count();
                foreach (Match match in matches)
                {
                    if (match.Groups[0] != null)
                    {
                        Array.Resize(ref this[strSubCom].subsets, this[strSubCom].subsets.Count() + 1);
                        this[strSubCom].subsets[i] = new List.SUBSETS();
                        this[strSubCom].subsets[i].name = match.Groups["subset"].Value;
                        if (match.Groups[1] != null)
                        {
                            this[strSubCom].subsets[i].eq = match.Groups["subeq"].Value;
                        }
                        i++;
                    }
                }

            }
        }
    }
}